Ext.define('pm.controller.Personal', {
    extend: 'Ext.app.Controller',

    views: [
        'Personal.List',
        'Personal.Edit',
        'Personal.Tree',
        'Personal.Filter',
        'Personal.UserProjects',

        'Project.List',
        'Project.Edit',
        'Project.ProjectMembers',

        'Basket'
    ],

    stores: [
        'Personal',
        'PersonalStruct',
        'Projects',
        'UserProjects',
        'UsersOfProject'
    ],

    models: [
        'Personal',
        'Project',
        'UserProject'
    ],

    userForms: [],
    projectForms: [],
    usersOfProjectLists: [],
    userProjectsListRender: [],

    init: function() {
        console.log('Initialized Users! This happens before the Application launch function is called');
        this.control({
            'PersonalList':{
                itemdblclick: this.startEditUser,
                addProjectToUser: this.addProjectToUser,
                selectionchange: this.onUserItemSelected
            },
            'ProjectsList':{
                //itemmouseenter: this.showProjectToolTip,
                addUserToProject: this.addUserToProject,
                selectionchange: this.onProjectItemSelected
            },
            'PersonalEdit': {
                render: this.userFormEdit,
                save: this.saveUser
            },
            'ProjectEdit': {
                render: this.projectFormEdit,
                save: this.saveProject
            },
            'ProjectMembersList': {
                render: this.onProjectMembersListRender,
                addUserToProject: this.addUserToProject,                
                selectionchange: this.onUserItemSelected
            },
            'UserProjectsList':{
                render: this.onUserProjectsListRender,
                addProjectToUser: this.addProjectToUser,                
                selectionchange: this.onProjectItemSelected
            }
        });
    },

    onProjectMembersListRender: function(v) {
        this.usersOfProjectLists.push(v)
    },

    onUserProjectsListRender: function(v) {
        this.userProjectsListRender.push(v)
    },

    userFormEdit: function(v) {
        this.userForms.push(v)
    },

    projectFormEdit: function(v) {
        this.projectForms.push(v)
    },

    addUserToProject: function(user, project) {
        this.addProjectToUser(project, user)
    },
    addProjectToUser: function(project, user) {
        console.log('addProjectToUser: user ' + user.get('name') + ', project ' + project.get('name'))
        var userProjectsStore = Ext.getStore('UserProjects')
        var usersOfProjectStore = Ext.getStore('UsersOfProject')

        var userProjects = Ext.create('pm.model.UserProject');
        userProjects.set('project_id', project.get('id'))
        userProjects.set('user_id', user.get('id'))
        userProjects.set('project_name', project.get('name'))
        userProjects.set('user_name', user.get('name'))
        userProjectsStore.add(userProjects)
        usersOfProjectStore.add(userProjects)
    },

    startEditUser: function(grid, record) {
        Ext.Array.each(this.userForms, function(view) {
            view.setRecord(record)
        })
    },

    startEditProject: function(grid, record) {
        Ext.Array.each(this.projectForms, function(view) {
            view.setRecord(record)
        })
    },

    showProjectToolTip: function(grid, record, item, index, e) {
        var view = Ext.getCmp('AllProjectsList');
        view.showToolTip(record, e);
    },

    saveUser: function(win) {
        console.log('clicked the Save button');
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();
        record.set(values);
    },

    saveProject: function(win) {
        console.log('clicked the Save button');
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();
        record.set(values);
    },

    onUserItemSelected: function(model, records) {
        if (records[0]) {
            this.startEditUser(null, records[0])
        }
    },
    onProjectItemSelected: function(model, records) {
        if (records[0]) {
            this.startEditProject(null, records[0])
        }
    },
});
